package com.niiwoo.civet.user.service.duboo;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.user.dao.entity.UserInstationLetter;
import com.niiwoo.civet.user.dto.request.MarketingJPushRequestDTO;
import com.niiwoo.civet.user.dto.request.MarketingPushLetterRequestDTO;
import com.niiwoo.civet.user.dto.request.SearchUserLetterListRequestDTO;
import com.niiwoo.civet.user.dto.request.UserLetterRequestDTO;
import com.niiwoo.civet.user.dto.response.QueryUserLetterExtInfoResponseDTO;
import com.niiwoo.civet.user.dto.response.QueryUserLetterListResponseDTO;
import com.niiwoo.civet.user.enums.UserLetterTypeEnum;
import com.niiwoo.civet.user.service.UserLetterDubboService;
import com.niiwoo.civet.user.service.local.UserLetterService;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户站内信与极光推送服务
 * @author xueyuke
 * @date 2017-12-16
 */
@Service(version = "1.0.0")
@Slf4j
public class UserLetterDubboServiceImpl implements UserLetterDubboService {
    @Autowired
    private UserLetterService userLetterService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Override
    public void pushLetter(UserLetterRequestDTO userLetterRequestDTO) {
        //仅发送站内信;
        if(UserLetterTypeEnum.ONLY_LETTER.equals(userLetterRequestDTO.getUserLetterTypeEnum())){
            userLetterService.pushLetter(userLetterRequestDTO);
        }
        //仅发送极光推送;
        else if(UserLetterTypeEnum.ONLY_JPUSH.equals(userLetterRequestDTO.getUserLetterTypeEnum())){
            userLetterService.jpush(userLetterRequestDTO);
        }
        //同时发送站内信与极光推送;
        else if(UserLetterTypeEnum.LETTER_AND_JPUSH.equals(userLetterRequestDTO.getUserLetterTypeEnum())){
            userLetterService.pushLetter(userLetterRequestDTO);
            userLetterService.jpush(userLetterRequestDTO);
        }
    }

    @Override
    public PageResponseDTO<QueryUserLetterListResponseDTO> queryUserLetterList(SearchUserLetterListRequestDTO searchUserLetterListRequestDTO){
        PageResponseDTO<QueryUserLetterListResponseDTO> queryUserLetterList=new PageResponseDTO<QueryUserLetterListResponseDTO>();
        List<QueryUserLetterListResponseDTO> queryUserLetterListResponseDTOList=new ArrayList<QueryUserLetterListResponseDTO>();

        List<UserInstationLetter> userInstationLetterList=userLetterService.queryUserLetterList(searchUserLetterListRequestDTO);
        Integer userLetterCount=userLetterService.queryUserLetterCount(searchUserLetterListRequestDTO);
        log.info("查询站内信列表,用户ID={},查询结果={}", searchUserLetterListRequestDTO.getUserId(),JSON.toJSONString(userInstationLetterList));
        for(UserInstationLetter userInstationLetter:userInstationLetterList){
            QueryUserLetterListResponseDTO queryUserLetterListResponseDTO=new QueryUserLetterListResponseDTO();
            queryUserLetterListResponseDTO.setId(userInstationLetter.getId());
            queryUserLetterListResponseDTO.setTitle(userInstationLetter.getTitle());
            queryUserLetterListResponseDTO.setSendDate(userInstationLetter.getSendDate());
            queryUserLetterListResponseDTO.setToUserId(userInstationLetter.getToUserId());
            queryUserLetterListResponseDTO.setIsRead(userInstationLetter.getIsRead());
            queryUserLetterListResponseDTO.setViewDate(userInstationLetter.getViewDate());
            queryUserLetterListResponseDTO.setCreateTime(userInstationLetter.getCreateTime());
            queryUserLetterListResponseDTO.setUpdateTime(userInstationLetter.getUpdateTime());
            queryUserLetterListResponseDTO.setContent(userInstationLetter.getContent());
            queryUserLetterListResponseDTO.setButtonList(userInstationLetter.getAppbuttons());

            queryUserLetterListResponseDTOList.add(queryUserLetterListResponseDTO);
        }

        queryUserLetterList.setItems(queryUserLetterListResponseDTOList);
        queryUserLetterList.measureTotalPage(userLetterCount,searchUserLetterListRequestDTO.getPageSize());

        //查询站内信列表的时候，需要将消息主界面的通知栏红点数清除;
        cleanRedPoint(searchUserLetterListRequestDTO.getUserId());
        return queryUserLetterList;
    }

    @Override
    public QueryUserLetterExtInfoResponseDTO queryUserLetterExtInfo(String userId){
        QueryUserLetterExtInfoResponseDTO queryUserLetterExtInfoResponseDTO=new QueryUserLetterExtInfoResponseDTO();
        UserInstationLetter userInstationLetter=userLetterService.queryUserLastOneLetter(userId);
        if(userInstationLetter!=null){
            queryUserLetterExtInfoResponseDTO.setLastUserLetter(transUserLetter(userInstationLetter));
        }

        //根据时间查询用户的红点数;
        queryUserLetterExtInfoResponseDTO.setRedPointCount(userLetterService.queryRedPointCountByUserIdAndDate(userId));

        return queryUserLetterExtInfoResponseDTO;
    }

    @Override
    public void readLetter(Long userLetterId) {
        userLetterService.readLetter(userLetterId);
    }

    @Override
    public void deleteLetter(List<Long> userLetterIdList) {
        userLetterService.deleteLetter(userLetterIdList);
    }

    @Override
    public void cleanRedPoint(String userId) {
        userLetterService.deleteRedPointByUseId(userId);
    }

    public QueryUserLetterExtInfoResponseDTO.UserLetter transUserLetter(UserInstationLetter userInstationLetter){
        QueryUserLetterExtInfoResponseDTO.UserLetter userLetter=new QueryUserLetterExtInfoResponseDTO.UserLetter();
        userLetter.setId(userInstationLetter.getId());
        userLetter.setTitle(userInstationLetter.getTitle());
        userLetter.setSendDate(userInstationLetter.getSendDate());
        userLetter.setToUserId(userInstationLetter.getToUserId());
        userLetter.setIsRead(userInstationLetter.getIsRead());
        userLetter.setViewDate(userInstationLetter.getViewDate());
        userLetter.setMessageType(userInstationLetter.getMessageType());
        userLetter.setCreateTime(userInstationLetter.getCreateTime());
        userLetter.setUpdateTime(userInstationLetter.getUpdateTime());
        userLetter.setContent(userInstationLetter.getContent());
        userLetter.setButtonList(userInstationLetter.getAppbuttons());

        return userLetter;
    }

    @Override
    public void pushMarketingLetter(MarketingPushLetterRequestDTO userLetterRequestDTO) {
        userLetterService.pushMarketingLetter(userLetterRequestDTO);
    }

    @Override
    public void pushMarketingJpush(MarketingJPushRequestDTO jPushRequestDTO){
        userLetterService.marketingJpush(jPushRequestDTO);
    }
}
